﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using TPEngine;
using TPEngine.Controls;
using Microsoft.Xna.Framework.Content;

namespace Josse.GameStates
{
    public class OptionsScreen : BaseGameState
    {
        #region Field region

        PictureBox backgroundImage;
        PictureBox arrowImage;

        LinkLabel fullscreen;
        LinkLabel backToMenu;

        float maxItemWidth = 0f;

        string isFullscreen;

        #endregion

        #region Property Region


        #endregion

        #region Constructor Region

        public OptionsScreen(Game game, GameStateManager manager)
            : base(game, manager)
        {
        }

        #endregion

        #region XNA Method Region

        public override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            base.LoadContent();

            if (GameRef.graphics.IsFullScreen == true)
                isFullscreen = "On";
            else if (GameRef.graphics.IsFullScreen == false)
                isFullscreen = "Off";

            ContentManager Content = Game.Content;

            backgroundImage = new PictureBox(
                Content.Load<Texture2D>(@"Backgrounds\bg2"),
                GameRef.ScreenRectangle);
            ControlManager.Add(backgroundImage);

            Texture2D arrowTexture = Content.Load<Texture2D>(@"GUI\leftarrow");
            Texture2D rightArrow = Content.Load<Texture2D>(@"GUI\rightarrow");

            arrowImage = new PictureBox(
                arrowTexture,
                new Rectangle(
                    0,
                    0,
                    arrowTexture.Width,
                    arrowTexture.Height));
            ControlManager.Add(arrowImage);

            fullscreen = new LinkLabel();
            fullscreen.Text = "Fullscreen:           " + isFullscreen;
            fullscreen.Size = fullscreen.SpriteFont.MeasureString(fullscreen.Text);
            fullscreen.Selected += menuItem_Selected;

            backToMenu = new LinkLabel();
            backToMenu.Text = "Quit";
            backToMenu.Size = backToMenu.SpriteFont.MeasureString(backToMenu.Text);
            backToMenu.Selected += menuItem_Selected;

            ControlManager.Add(fullscreen);
            ControlManager.Add(backToMenu);

            ControlManager.NextControl();

            ControlManager.FocusChanged += new EventHandler(ControlManager_FocusChanged);
            Vector2 position = new Vector2(350, 500);

            foreach (Control c in ControlManager)
            {
                if (c is LinkLabel)
                {
                    if (c.Size.X > maxItemWidth)
                        maxItemWidth = c.Size.X;

                    c.Position = position;
                    position.Y += c.Size.Y + 5f;
                }
            }

            ControlManager_FocusChanged(fullscreen, null);
        }

        void ControlManager_FocusChanged(object sender, EventArgs e)
        {
            Control control = sender as Control;
            Vector2 position = new Vector2(control.Position.X + maxItemWidth + 10f,
                control.Position.Y);

            arrowImage.SetPosition(position);
        }

        void menuItem_Selected(object sender, EventArgs e)
        {
            if (sender == fullscreen)
                ApplyFullscreen();
            if (sender == backToMenu)
                StateManager.ChangeState(GameRef.startMenuScreen);
        }

        public override void Update(GameTime gameTime)
        {
            ControlManager.Update(gameTime);

            fullscreen.Text = "Fullscreen:           " + isFullscreen;

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            GameRef.SpriteBatch.Begin();

            base.Draw(gameTime);

            ControlManager.Draw(GameRef.SpriteBatch);

            GameRef.SpriteBatch.End();
        }

        #endregion

        #region Game State Method Region

        private void ApplyFullscreen()
        {
            if (GameRef.graphics.IsFullScreen == true)
            {
                GameRef.graphics.IsFullScreen = false;
                isFullscreen = "Off";
            }
            else
            {
                GameRef.graphics.IsFullScreen = true;
                isFullscreen = "On";
            }

            GameRef.graphics.ApplyChanges();
        }

        #endregion
    }
}
